<template>
  <div class="saveUpdate">
    <el-dialog :close-on-click-modal="false" :title="id?'编辑公告':'新增公告'" :visible.sync="showDialog" @close="$emit('update:showUpdate', false)"
               :showUpdate="showUpdate" width="500px">
      <el-form :model="DetailInfo" ref="saveChn" label-width="120px" size="mini">
        <dialog-wrapper :MaxWidth="400">
          <el-form-item label="关联商户：">
            <el-select v-model="DetailInfo.mch_ids" clearable multiple placeholder="请选择">
              <el-option v-for="item in MerchantsList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="公告内容：" prop="value" :rules="[{ required: true, message: '公告内容不能为空'}]">
            <el-input type="textarea" :rows="8" v-model="DetailInfo.value" width="200px"></el-input>
          </el-form-item>
          <el-form-item label="开始时间：" prop="start_time" :rules="[{ required: true, message: '开始时间不能为空'}]">
            <el-date-picker style="width: 280px" :picker-options="pickerOptions" v-model="DetailInfo.start_time"
                            value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间：" prop="end_time" :rules="[{ required: true, message: '结束时间不能为空'}]">
            <el-date-picker class="pickerStyle" :picker-options="pickerOptions" v-model="DetailInfo.end_time"
                            value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
        </dialog-wrapper>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:showUpdate', false)" size="small">取 消</el-button>
        <el-button v-if="id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定</el-button>
        <el-button v-else type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DialogWrapper from '@/components/DialogWrapper'
  import {getBulletinDetail, saveBulletin, updateBulletin} from '@/api'
  import {useVcode} from "../../utils/CommonJs";

  export default {
    components: {DialogWrapper},
    data() {
      return {
        setbank: false,
        showDialog: this.showUpdate,
        loading: false,
        DetailInfo: {},
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7
          }
        }
      }
    },
    props: {
      showUpdate: {
        type: Boolean,
        default: false
      },
      Detail: {
        type: Object
      },
      BulletinType: {
        type: Object
      },
      settleCycle: {
        type: Object
      },
      MerchantsList: {
        type: Array
      },
      id: {
        type: Number
      },
    },

    created() {
      if (this.id) {
        this.getDetail()
      }else{
        this.mchidDefaultValue();
      }
    },
    methods: {
        mchidDefaultValue(){
              let mchids=[];
                this.MerchantsList.forEach(item => {
                  mchids.push(item.id)
                })
              this.$set(this.DetailInfo,'mch_ids',mchids)
            },
      saveDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            useVcode(this,()=>{
              saveBulletin(this.DetailInfo).then(res => {
                if (res.status === 0) {
                  this.$message({message: '新增成功。', type: 'success'})
                  this.$set(this.DetailInfo,'mch_ids',[])
                  this.$emit('update:showUpdate', false)
                }
              })
            })
          }
        })
      },
      updateDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            useVcode(this,()=>{
              updateBulletin(this.DetailInfo, this.id).then(res => {
                if (res.status === 0) {
                  this.$message({message: '编辑成功。', type: 'success'})
                  this.$set(this.DetailInfo,'mch_ids',[])
                  this.$emit('update:showUpdate', false)
                }
              })
            })
          }
        })
      },
      getDetail() {
        getBulletinDetail(this.id).then(res => {
          if (res.status === 0) {
            this.DetailInfo = res.data
            this.$set(this.DetailInfo,'mch_ids',[])
            let mchids=[];
            res.data.mchs.forEach(item => {
              mchids.push(item.id)
            })
            this.$set(this.DetailInfo,'mch_ids',mchids)
          }
        })
      }
    },
    watch: {
      showUpdate() {
        this.showDialog = this.showUpdate
      },
    }
  }
</script>


<style scoped>
  .pickerStyle {
    width: 280px !important;
  }

</style>
